package com.date;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

/**
 * @Description: Java 8中时间类的使用
 */
public class Java8Demo {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        System.out.println("今天日期："+today);

        //今天日期，只有包含日期
        int year = today.getYear();
        int month = today.getMonthValue();
        int day = today.getDayOfMonth();
        System.out.println("今天日期："+year+"/"+month+"/"+day);

        //获得时间
        LocalTime localTime = LocalTime.now();
        System.out.println("现在时间："+localTime);

        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("现在日期和时间："+localDateTime);

        //指定年月日输出
        LocalDate date1 = LocalDate.of(2020,8,6);
        System.out.println("指定日期："+date1);

        //equals方法
        System.out.println("今天是否为2020-06-06："+today.equals(date1));

        MonthDay monthToDay = MonthDay.of(today.getMonth(),today.getDayOfMonth());
        MonthDay monthToDay1 = MonthDay.from(today);
        System.out.println("MonthDay只有月，日："+monthToDay);

        //获得2个小时后时间
        LocalTime twoHour = localTime.plusHours(2);
        System.out.println("2个小时后时间："+twoHour);

        //修改日期
        LocalDate oneWeek = today.plusWeeks(2);
        System.out.println("2周后日期："+oneWeek);

        LocalDate pYear = today.minus(1, ChronoUnit.YEARS);
        System.out.println("1年前日期："+pYear);

        LocalDate nYear = today.plus(1, ChronoUnit.YEARS);
        System.out.println("1年后日期："+nYear);

        Clock clock = Clock.systemUTC();
        System.out.println("Clock："+clock);
        Clock clock1 = Clock.systemDefaultZone();
        System.out.println("Clock："+clock1);

        ZoneId zoneId = ZoneId.of(ZoneId.SHORT_IDS.get("ACT"));
        ZonedDateTime nowDateTimeInAustralia = ZonedDateTime.of(localDateTime,zoneId);
        System.out.println("澳大利亚时区时间：："+nowDateTimeInAustralia);

        //检查闰年
        System.out.println("今年是否为闰年："+today.isLeapYear());

        //两个日期之间包含多少天，多少月
        Period period = Period.between(today,date1);
        System.out.println("今天和"+date1+"相差"+period.getMonths()+"月"+period.getDays()+"天");

        //利用时区偏移量获得新时间
        ZoneOffset zoneOffset = ZoneOffset.of("+05:40");
        OffsetDateTime date = OffsetDateTime.of(localDateTime,zoneOffset);
        System.out.println("日期时间时区偏移后："+date);

        //当前时间戳
        Instant timestamp = Instant.now();
        System.out.println("当前时间戳："+timestamp);

        //解析日期
        String date2 = "20201109";
        LocalDate date2Format = LocalDate.parse(date2, DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println("字符转日期："+date2Format);

        //自定义解析日期2
        String date3 = "2020-10-09";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date4 = LocalDate.parse(date3,formatter);
        System.out.println("自定义字符转日期："+date4);

        //格式化日期
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss a");
        String nowDate = localDateTime.format(formatter1);
        System.out.println("日期转字符："+nowDate);

    }
}
